﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Outbound
{
    public partial class SelectItemCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string sql = @"SELECT T0.ItemCode, T2.ItemName
                            FROM OITW T0
                            JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
                            LEFT JOIN OITM T2 ON T0.ItemCode = T2.ItemCode
                            WHERE T1.U_Class1=103 AND T1.U_Class2=206
                            AND T0.Onhand>0";

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string itemCode = gridView.DataKeys[gridViewRow.RowIndex]["ItemCode"].ToString();
            string itemName = gridView.DataKeys[gridViewRow.RowIndex]["ItemName"].ToString();

            switch (e.CommandName)
            {
                case "CmdSelect":
                    string returnValue = itemCode + "|" + itemName;
                    Response.Write("<script>window.returnValue='" + returnValue + "';window.opener = null;window.open('', '_self', '');window.close();</script>");
                    break;
                default:
                    break;
            }
        }
    }
}